package service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import model.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import tool.ExcelWorkBook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author： 杨浩然
 * @date： 2023-02-02 10:13
 */
public class ExcelHandleServiceImpl {

    /**
     * 导出文件，导出到指定路径
     *
     * @param pathExport 绝对路径
     * @return
     */
    public boolean excelExportFile(String pathExport, ExcelWorkBook excelWorkBook) {
        HSSFWorkbook workBook = excelWorkBook.getWorkBook();
        try {
            File file = new File(pathExport);
            if (!file.exists()) {
                file.mkdir();
            }
//            String path = file.getPath() + "\\" + "\\对照表" + DateUtil.now() + ".xls";
            String path = file.getPath() + "\\" + "\\对照表.xls";
            FileOutputStream outputStream = new FileOutputStream(path);
            //将文件写到磁盘对应的位置
            workBook.write(outputStream);

            System.out.println("文件已经导出 路径："+file.getPath()+"\\" + "\\对照表.xls");
            outputStream.flush();
            outputStream.close();
            workBook.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        workBook = new HSSFWorkbook();
//        List<Map<String, Object>> listMap1 = dbHelper.getListMap(strSql1, null);
//        List<Map<String, Object>> listMap2 = dbHelper.getListMap(strSql2, null);
//
//        //生成 省(区,市)办公用房sheet
//        creatSheetQuXian1(listMap1);
//        //生成 技术业务用房sheet
//        creatSheet2(listMap2);
//        if (workBook != null) {
//            String fileName = "市级数据上报信息表.xls";
//            response.setHeader("Content-Type", "application/octet-stream");
//            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//            OutputStream out = response.getOutputStream();
//            workBook.write(out);
//            out.close();
//        }

        return false;
    }

    /**
     * 处理数据，转化为excel类
     *
     * @param
     * @return
     */
    public ExcelWorkBook excelHandle(List<DatabaseJsonModel> djmList) {
        ExcelWorkBook excelWorkBook = new ExcelWorkBook();
        for (int i = 0; i < djmList.size(); i++) {
            DatabaseJsonModel databaseJsonModel = djmList.get(i);
            List<DatabaseFrom> dataFromList = databaseJsonModel.getDataFrom();
            excelWorkBook.sheetAt(databaseJsonModel.getNum(), databaseJsonModel.getDatabaseName());
            //写入错误消息
            excelWorkBook.getNextRowCell().setCellValue("重要提示：");
            excelWorkBook.setRowDataErr(databaseJsonModel.getErrmsg());
            excelWorkBook.addIntervalNum();//增加间隔行

            DatabaseConnectParameter lydata = databaseJsonModel.getDcpContrast();
            if (databaseJsonModel.getNum()==0){
                lydata=databaseJsonModel.getDcpStandard();
            }
            excelWorkBook.getNextRowCell().setCellValue("数据来源:"+lydata.getConnecturl());
            excelWorkBook.getNextRowCell().setCellValue("库：:"+lydata.getSchemaPattern());
            //写入当前数据库表目录
            excelWorkBook.getNextRowCell().setCellValue("表目录：");

            for (int j = 0; j < dataFromList.size(); j++) {
                excelWorkBook.getNextRowCell().setCellValue(String.valueOf(j+1)+"、"+dataFromList.get(j).getFromName());
            }
            excelWorkBook.addIntervalNum();//增加间隔行


            //写入表消息
            for (DatabaseFrom df : dataFromList) {
                excelFromHandle(excelWorkBook, df,databaseJsonModel);
                excelWorkBook.addIntervalNum();
            }

        }

        return excelWorkBook;
    }

    private void excelFromHandle(ExcelWorkBook excelWorkBook, DatabaseFrom databaseFrom,DatabaseJsonModel databaseJsonModel) {
        //表名
        excelWorkBook.getNextRowCell().setCellValue("表名：" + databaseFrom.getFromName());
        //对照表名
        if (databaseJsonModel.getNum()!=0){
            excelWorkBook.getNextRowCell().setCellValue("标准库对应表名：" + databaseJsonModel.getFrombinding().get(databaseFrom.getFromName()));
        }

        //列名 后面整合下
        excelWorkBook.getNextRowCell().setCellValue("列名称");
        excelWorkBook.getNextCell().setCellValue("类型名");
        excelWorkBook.getNextCell().setCellValue("列的大小");
        excelWorkBook.getNextCell().setCellValue("小数部分的位数");
        excelWorkBook.getNextCell().setCellValue("是否允许使用NULL");
        excelWorkBook.getNextCell().setCellValue("对照信息");
        excelWorkBook.getNextCell().setCellValue("描述列的注释");
        //列信息
        for (DatabaseFromColumn dfc : databaseFrom.getDataColumn()) {
            excelWorkBook.nextRow();
            excelWorkBook.getNextCell().setCellValue(dfc.getCOLUMN_NAME());
            excelWorkBook.getNextCell().setCellValue(dfc.getTYPE_NAME());
            excelWorkBook.getNextCell().setCellValue(dfc.getCOLUMN_SIZE());
            excelWorkBook.getNextCell().setCellValue(dfc.getDECIMAL_DIGITS());
            excelWorkBook.getNextCell().setCellValue(dfc.getNULLABLE());

            HSSFCell nextCell = excelWorkBook.getNextCell();
            EqualsMessage equalsMessage = dfc.getEqualsMessage();
            if (equalsMessage != null) {
                nextCell.setCellStyle(excelWorkBook.getstyles(dfc.getEqualsMessage().getErrlevel()));
                nextCell.setCellValue(dfc.getEqualsMessage().getMsg());
            }
            excelWorkBook.getNextCell().setCellValue(dfc.getREMARKS());

        }
    }


}
